import { ApiProperty, IntersectionType, PartialType } from '@nestjs/swagger'
import {
  IsNumber,
  IsOptional,
  IsString,
  MinLength,
} from 'class-validator'

import { OperatorDto } from '~/common/dto/operator.dto'
import { PagerDto } from '~/common/dto/pager.dto'

export class DeviceDto extends OperatorDto {
  @ApiProperty({ description: '设备ID' })
  @IsOptional()
  uid?: string

  @ApiProperty({ description: '序列号' })
  @MinLength(10, { message: '序列号长度不能小于10' })
  sn: string

  @ApiProperty({ description: '设备名称' })
  @IsOptional()
  name?: string

  @ApiProperty({ description: '设备类型' })
  @IsOptional()
  type?: number

  @ApiProperty({ description: '用户ID' })
  @IsOptional()
  userId?: number
}

export class DeviceUpdateDto extends PartialType(DeviceDto) {}

export class DeviceQueryDto extends IntersectionType(PagerDto, PartialType(DeviceDto)) {
  @ApiProperty({ description: '设备ID', required: false })
  @IsOptional()
  @IsString()
  uid?: string

  @ApiProperty({ description: '序列号', required: false })
  @IsOptional()
  @IsString()
  sn?: string

  @ApiProperty({ description: 'MAC地址', required: false })
  @IsOptional()
  @IsString()
  mac?: string

  @ApiProperty({ description: '设备名称', required: false })
  @IsOptional()
  name?: string

  @ApiProperty({ description: '设备类型', required: false })
  @IsOptional()
  type?: number

  @ApiProperty({ description: '连接状态', required: false })
  @IsOptional()
  connectState?: number

  @ApiProperty({ description: '穿戴状态', required: false })
  @IsOptional()
  wearStatus?: number

  @ApiProperty({ description: '用户ID', required: false })
  @IsOptional()
  userId?: number

  @ApiProperty({ description: '部门ID', required: false })
  @IsOptional()
  deptId?: number
}

export class DeviceAuthDto {
  @ApiProperty({ description: 'MAC地址' })
  @IsString()
  mac: string

  @ApiProperty({ description: '序列号' })
  @IsString()
  sn: string
}

export class DeviceSignDto {
  @ApiProperty({ description: '时间戳' })
  @IsNumber()
  ts: number

  @ApiProperty({ description: '签名' })
  @IsString()
  sign: string
}

export class DeviceAttrDto extends OperatorDto {
  @ApiProperty({ description: '设备ID' })
  @IsOptional()
  @IsString()
  uid?: string

  @ApiProperty({ description: 'UUID' })
  @IsOptional()
  @IsString()
  uuid: string

  @ApiProperty({ description: '设备MAC' })
  @IsOptional()
  @IsString()
  mac: string

  @ApiProperty({ description: '设备名' })
  @IsOptional()
  @IsString()
  name: string

  @ApiProperty({ description: '版本号' })
  @IsOptional()
  @IsString()
  version: string

  @ApiProperty({ description: '功能' })
  @IsOptional()
  @IsString()
  capability: string

  @ApiProperty({ description: '连接状态' })
  @IsOptional()
  @IsNumber()
  connectState: number

  @ApiProperty({ description: '佩戴状态' })
  @IsOptional()
  @IsNumber()
  wearStatus: number

  @ApiProperty({ description: '电池电量' })
  @IsOptional()
  @IsNumber()
  battery: number

  @ApiProperty({ description: '更新时间' })
  @IsOptional()
  updatedAt?: Date
}

export class DeviceConfigDto {
  @ApiProperty({ description: '类型' })
  @IsNumber()
  type: number

  @ApiProperty({ description: '数值' })
  @IsNumber()
  value: number

  @ApiProperty({ description: '状态' })
  @IsNumber()
  status: number
}

export class AlarmRuleDto {
  @ApiProperty({ description: 'ID' })
  @IsNumber()
  id: number

  @ApiProperty({ description: '类型' })
  @IsNumber()
  type: number

  @ApiProperty({ description: '最小数值' })
  @IsNumber()
  min: number

  @ApiProperty({ description: '最大数值' })
  @IsNumber()
  max: number

  @ApiProperty({ description: '状态' })
  @IsNumber()
  status: number
}
